package com.situ.wms.controller;

import com.situ.wms.util.ImageUtil;

import javax.imageio.ImageIO;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

 @WebServlet("/image")
public class ImageController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("image/jpeg");
        //禁止图片缓存
        response.setHeader("Pragma","No-cache");
        response.setHeader("Cache-Control","no-cache");
        response.setDateHeader("Expires",0);
        ServletOutputStream sos = response.getOutputStream();
        String randomImag="";
        int i=0;
        while (i<4){
            int ran =(int)(Math.random()*52)+1;
            if(ran<=26){
                randomImag +=(char)(ran+64);
            }else {
                randomImag +=(char)(ran+70);
            }
            i++;
        }
        //流写出图像
        request.getSession().setAttribute("captchaSession",randomImag);

        ImageIO.write(ImageUtil.image(randomImag),"JPEG",sos);

        sos.flush();
        sos.close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
